<?php
/**
*
* @package AL_Facebook_Comments
* @copyright (C)2011 AlexLopezIT.com
* @license GNU/GPL
* http://www.AlexLopezIT.com
*
**/
// no direct access
defined( '_JEXEC' ) or  die('Restricted access');
jimport( 'joomla.event.plugin' );
class plgContental_facebook_comments extends JPlugin
{
	function plgContental_facebook_comments( &$subject, $params )
	{
		parent::__construct( $subject, $params );
	}

	// Funcion que hace todo por ahora
	function plgContentALFacebookComments(&$article, $appId, $num_posts, $width, $idioma, $url_img, $colorscheme, $likebtn, $support, $admins, $tweet, $gplus, $sharebtn, $socialpos)
	{
		//$lang =& JFactory::getLanguage();
		//$locales = $lang->getLocale();
		//$idiom = substr($locales[0], 0, 5);
		if($idioma == 'es'){
			$al = 'Caja de comentarios de Facebook para Joomla';
		}else{
			$al = 'Facebook Social Comments Box for Joomla';
		}
		if($idioma == 'en'){
			$g_lang = 'en';
			$t_lang = 'en-US';
			$f_lang = 'en-US';
		}
		if($idioma == 'es'){
			$g_lang = 'es';
			$t_lang = 'es';
			$f_lang = 'es_ES';
		}
		if($idioma == 'it'){
			$g_lang = 'it';
			$t_lang = 'it';
			$f_lang = 'it_IT';
		}
		if($idioma == 'fr'){
			$g_lang = 'fr';
			$t_lang = 'fr';
			$f_lang = 'fr_FR';
		}
		if($idioma == 'de'){
			$g_lang = 'de';
			$t_lang = 'de';
			$f_lang = 'de_DE';
		}
		if($idioma == 'pt'){
			$g_lang = 'pt-BR';
			$t_lang = 'pt';
			$f_lang = 'pt_PT';
		}
		if($idioma == 'ja'){
			$g_lang = 'ja';
			$t_lang = 'ja';
			$f_lang = 'ja_JP';
		}
		if($idioma == 'ko'){
			$g_lang = 'ko';
			$t_lang = 'ko';
			$f_lang = 'ko_KR';
		}
		if($idioma == 'ru'){
			$g_lang = 'ru';
			$t_lang = 'ru';
			$f_lang = 'ru_RU';
		}
		if($idioma == 'tr'){
			$g_lang = 'tr';
			$t_lang = 'tr';
			$f_lang = 'tr_TR';
		}

		global $mainframe;
		if($appId != '')
		{		
			$fbshare = '<a name="fb_share" type="button" share_url="'.$this->getArticleRoute($article).'" href="http://www.facebook.com/sharer.php">Share</a><script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"></script>';
			$googleplus = '<script type="text/javascript" src="https://apis.google.com/js/plusone.js">{lang: "'.$g_lang.'"}</script><g:plusone size="medium"></g:plusone>';
			$twitter = '<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-lang="'.$t_lang.'">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>';
			$fblike = '<fb:like href="'.$this->getArticleRoute($article).'" send="false" layout="button_count" width="180" show_faces="false" font=""></fb:like>';
			$fbsend = '<fb:like href="'.$this->getArticleRoute($article).'" send="true" layout="button_count" width="180" show_faces="false" font=""></fb:like>';
			
			$sharebar .= '<div class="al_share_bar" style="display:block;height:20px;margin-bottom:10px;">';
			if($gplus == '1')
			{
				$sharebar .= '<div style="width:75px; float:left;" id="al_gplus">'.$googleplus.'</div>';
			}
			if($tweet == '1')
			{
				$sharebar .= '<div style="float:left;" id="al_tweet">'.$twitter.'</div>';
			}
			if($sharebtn == '1')
			{
				$sharebar .= '<div style="width:75px; float:left;" id="al_fbshare">'.$fbshare.'</div>';
			}
			if($likebtn == '2')
			{
				$sharebar .= '<div style="float:left;" id="al_fblike">'.$fblike.'</div>';
			}
			if($likebtn == '3')
			{
				$sharebar .= '<div style="float:left;" id="al_fblike">'.$fbsend.'</div>';
			}
			$sharebar .= '</div>';
			
			$desc = $article->metadesc;
			
			if($desc == ''){
				$desc = $mainframe->getCfg('MetaDesc');
			}
			$mainframe->addCustomHeadTag('<meta property="fb:app_id" content="'.$appId.'" />');
			$mainframe->addCustomHeadTag('<meta property="fb:admins" content="'.$admins.'">');
			
			//$mainframe->addCustomHeadTag('<meta property="og:image" content="'.$url_img.'"/>');
			$mainframe->addCustomHeadTag('<meta property="og:title" content="'.$article->title.'"/>');
			$mainframe->addCustomHeadTag('<meta property="og:url" content="'.$this->getArticleRoute($article).'"/>');
			$mainframe->addCustomHeadTag('<meta property="og:description" content="'.$desc.'"/>');
			$mainframe->addCustomHeadTag('<meta property="og:site_name" content="'.$mainframe->getCfg('sitename').'"/>');
			$mainframe->addCustomHeadTag('<meta property="og:type" content="article" />');
			
			$al_facebook_comments   = array();
			$al_facebook_comments[] = '<img src="'.$url_img.'" border="0" alt="'.$article->title.'" style="display:none" />';
			$al_facebook_comments[] = '<div class="al_facebook_comments" style="margin-top:22px;display:block;float: left;width: 100%;position: relative;">';
			$al_facebook_comments[] = '<div id="fb-root"></div>';
			$al_facebook_comments[] = '<script src="http://connect.facebook.net/'.$f_lang.'/all.js#appId='.$appId.'&amp;xfbml=1"></script>';
			if($socialpos == '1'){
			$al_facebook_comments[] = $sharebar;
			}
			$al_facebook_comments[] = '<fb:comments xid="'.$appId.'" title="'.$article->title.'" href="'.$this->getArticleRoute($article).'" num_posts="'.$num_posts.'" width="'.$width.'" publish_feed="true" colorscheme="'.$colorscheme.'" ></fb:comments>';
			if($support == '1'){
			$al_facebook_comments[] = '<div style="font-size:9px;"><a href="http://www.alexlopezit.com/joomla/al-facebook-comments-box-for-joomla">'.$al.'</a></div>';
			}
			if($support == '2'){
			$al_facebook_comments[] = '<div style="display:none;"><a href="http://www.alexlopezit.com/joomla/al-facebook-comments-box-for-joomla">'.$al.'</a></div>';
			}
			if($support == '0'){
			$al_facebook_comments[] = '<!-- '.$al.' http://www.alexlopezit.com/joomla/al-facebook-comments-box-for-joomla -->';
			}
			$al_facebook_comments[] = '</div>';
			
			if($socialpos == '0')
			{
				// Social bar at the top
				die('x');
				$article->text = $sharebar.$article->text.implode("\n", $al_facebook_comments);
			}else{
				// Social bar at the bottom
				//echo JUtility::dump($al_facebook_comments);die;
				$article->text = $article->text.implode("\n", $al_facebook_comments);
			}
		}
		else
		{
			$article->text = $article->text.implode("\n", "No Facebook appId");
		}
		unset($al_facebook_comments);
		return true;
	}

	function getArticleRoute(&$obj, $encode = 'False') {
		$url = 'Error, No URL';
		if (! is_null ( $obj )) {
			require_once (JPATH_ROOT . DS . 'components' . DS . 'com_content' . DS . 'helpers' . DS . 'route.php');
			$url = JRoute::_ ( ContentHelperRoute::getArticleRoute ($obj->slug, $obj->catslug, $obj->sectionid)); 
			$uri = & JURI::getInstance ();
			$base = $uri->toString ( array ('scheme', 'host', 'port' ) );
			$url = $base . $url;
			$url = JRoute::_ ( $url, true, 0 );
			if($encode == 'True'){
				return urlencode(htmlentities( $url ));
			}else {
				return  $url;
			}
		}
		return $url;
	}

	function plgShowInIntro(&$obj, &$params, &$inintro)
	{
		if ($inintro == '1')
		{
			return true;
		}else{
			if (JString::strlen(strip_tags($obj->fulltext)) !='0')
			{
				if ((JRequest :: getVar('view')) == 'article')
				{
					return true;				
				}else{
					return false;
				}
			}else{
				return true;
			}
		}
	}
	
	function plgContentCheckSecCatArt(&$row)
	{
		$plugin = & JPluginHelper::getPlugin('content', 'al_facebook_comments');
		$pluginParams  = new JParameter( $plugin->params );
		$pluginRegistry  = $pluginParams->_registry['_default']['data'];
		$value_sec = 0;
		$value_cat = 0;
		$value_art = 0;
		if ($pluginRegistry->sectionid !=''){
			// Check accepted section	
  			$aAcceptedSectionsArray = array();
  			$aAcceptedSectionsArray = explode(',',$pluginRegistry->sectionid);
  			if(in_array($row->sectionid, $aAcceptedSectionsArray) != true){
   				$value_sec = '1';
  			}
  			unset($aAcceptedSectionsArray);
  		}	

  		// Check accepted category
  		if ($pluginRegistry->catid !=''){
  			$aAcceptedCategoryArray = array();
  			$aAcceptedCategoryArray = explode(',',$pluginRegistry->catid);
  			if(in_array($row->catid, $aAcceptedCategoryArray) != true){
    				$value_cat = '1';
  			}
			unset($aAcceptedCategoryArray);
  		}
  			
   			// Check ignored articles
  		if ($pluginRegistry->articleid !=''){
	  		$aIgnoredArticleArray = array();
  			$aIgnoredArticleArray = explode(',',$pluginRegistry->articleid);
  			if(in_array($row->id, $aIgnoredArticleArray) ){
				$value_art = '1';
	  		}
	  		unset($aIgnoredArticleArray);
  		}
  		if( ($value_sec==1) || ($value_cat==1) || ($value_art==1) ){
  			return true;
  		}else{
  			return false;
  		}
	}

  	function plgAlFacebookCommentsShowSecCatFP($fron_page, $sec, $cat)
	{
		$view_ddrfs = JRequest :: getVar('view');
		// echo $view_ddrfs;
		switch ($view_ddrfs)
		{
			case 'article':
				return true;
				break;
			case 'frontpage':
				if ($fron_page == '1')
				{
					return true;
				}else{
					return false;
				}
				break;
			case 'section':
				if ($sec == '1')
				{
					return true;
				}else{
					return false;
				}
				break;
			case 'category':
				if ($cat == '1')
				{
					return true;
				}else{
					return false;
				}
				break;
		}
	}

	function onPrepareContent( &$article, &$params, $limitstart )
	{
		$plugin = & JPluginHelper::getPlugin('content', 'al_facebook_comments');
		$pluginParams  = new JParameter( $plugin->params );
		$pluginRegistry  = $pluginParams->_registry['_default']['data'];
		$sappId = $pluginRegistry->appId;
		$snum_posts = $pluginRegistry->num_posts;
		$sidioma = $pluginRegistry->idioma;
		$scolorscheme = $pluginRegistry->colorscheme;
		$swidth = $pluginRegistry->width;
		$surl_img = $pluginRegistry->url_img;
		$slikebtn = $pluginRegistry->likebtn;
		$ssupport = $pluginRegistry->support;
		$sadmins = $pluginRegistry->admins;
		$stweet = $pluginRegistry->tweet;
		$sgplus = $pluginRegistry->gplus;
		$ssharebtn = $pluginRegistry->sharebtn;
		$ssocialpos = $pluginRegistry->socialpos;
		
		if ($this->plgContentCheckSecCatArt($article) == true)
		{
			return true;
		}
		// Add Comments
		$sShowInIntro = $pluginRegistry->al_show_in_intro_text;
		$sShowFrontpage = $pluginRegistry->al_show_frontpage;
		$sShowSection = $pluginRegistry->al_show_section;
		$sShowCate = $pluginRegistry->al_show_cate;
		if ($this->plgAlFacebookCommentsShowSecCatFP($sShowFrontpage, $sShowSection, $sShowCate) == true)
		{
			if ($this->plgShowInIntro($article, $params, $sShowInIntro) == true)
			{
				$this->plgContentALFacebookComments($article, $sappId, $snum_posts, $swidth, $sidioma, $surl_img, $scolorscheme, $slikebtn, $ssupport, $sadmins, $stweet, $sgplus, $ssharebtn, $ssocialpos);
			}
		}
	}
}
?>